//
//  HMStatus.swift
//  Weibo
//
//  Created by HM on 16/5/23.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class HMStatus: NSObject {
    //自动匹配机型对应的平台
    //4 4s 5 5c 都是32的
    //之后的都是 64位
    var id: Int64 = 0
    ///微博信息内容
    var text: String?
    ///微博创建时间
    var created_at: String?
    ///微博来源
    var source: String?
    //用户  直接赋值 会给自定义对象赋值一个字典
    var user: HMUser?
    
    var retweeted_status: HMStatus?
    
    //转发的数量
    var reposts_count: Int = 0
    //评论
    var comments_count: Int = 0
    //赞
    var attitudes_count: Int = 0
    
    //配图视图的模型数组
    var pic_urls: [HMPicture]?
    
    //KVC 
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    override func setValue(value: AnyObject?, forKey key: String) {
    
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
                user = HMUser(dict: dict)
            }
            //需要添加return  不然字典转模型就白做了
            return
        }
        
        //给转发微博做字典转模型
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = HMStatus(dict: dict)
            }
            //需要添加return  不然字典转模型就白做了
            return
        }
        
        if key == "pic_urls" {
            //1.将value 转换为数组
            if let array = value as? [[String : AnyObject]] {
                //2.遍历数组
                var temparray = [HMPicture]()
                for item in array {
                     //3.字典转模型
                    let p = HMPicture(dict: item)
                    //添加到模型数组中
                    temparray.append(p)
                }
                //赋值保存
                pic_urls = temparray
            }
            return
        }
        //super在下面效率稍微高一点
        super.setValue(value, forKey: key)
    }
    
    //过滤
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {     }
    
}
